#ifndef	__DBAPI_INCLUDE__
#define	__DBAPI_INCLUDE__

class IDbRecordSet {
protected:
	virtual ~IDbRecordSet() {}

public:
	virtual void Release() = 0;

	virtual int FieldCount() = 0;
	virtual int RecordCount() = 0;

	virtual const char* FieldName(int nField) = 0;
	virtual const char* FieldValue(int nRow, int nField) = 0;

	template<typename T>
	bool GetFieldNumber(int nRow, int nField, T& Value) {
		const char* pText = FieldValue(nRow, nField);
		if(!pText) return false;
		double dValue = atof(pText);
		Value = (T)dValue;
		return true;
	}

	template<size_t nSize>
	bool GetFieldString(int nRow, int nField, char szValue[nSize]) {
		return GetFieldString(nRow, nField, szValue, nSize);
	}

	bool GetFieldString(int nRow, int nField, char* pValue, size_t nSize) {
		const char* pText = FieldValue(nRow, nField);
		if(!pText) return false;
		size_t nLen = strlen(pText);
		if(nLen>=nSize) return false;
		memcpy(pValue, pText, nLen+1);
		return true;
	}

};

class IDbClient {
protected:
	virtual ~IDbClient() {}

public:
	enum {
		DBERR_UNKNOWN = -1,
		DBERR_OK = 0,
		DBERR_NOT_FOUND = 1,
	};

	static void GlobalInit();
	static void GlobalFinal();
	static IDbClient* New(const char* pConnStr);

	virtual void Release() = 0;
	virtual bool IsAvaliable() = 0;
	virtual bool EscapeString(char* pTo, const char* pFrom, size_t nLength) = 0;

	virtual bool BeginTrans() = 0;
	virtual bool CommitTrans() = 0;
	virtual bool RollbackTrans() = 0;

	virtual IDbRecordSet* Query(const char* Sql, int nMax) = 0;
	virtual bool Execute(const char* Sql) = 0;

	virtual int ErrorCode() = 0;
	virtual const char* ErrorMsg() = 0;
};

#endif
